package com.zzh.myapplication.tools;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.widget.ImageView;

import net.icarplus.car.tools.ImageAsyncLoader.ImageCallback;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImageCacheManager {

	public static void setImageToImageView(Context context,
			ImageView thumbImageView, String strUrl, int defaultPicId) {
		final String url = strUrl;
		thumbImageView.setTag(url);
		Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
				defaultPicId);
		BitmapDrawable bitmapDrawable = ImageAsyncLoader.getInstance()
				.loadBitmapDrawable(
						url,
						thumbImageView,
						new ImageCallback() {

							@Override
							public void imageLoaded(
									BitmapDrawable imageDrawable,
									ImageView imageView, String imageUrl) {
								String curUrl = (String) imageView.getTag();
								if (imageUrl.equalsIgnoreCase(curUrl)) {
									imageView.setImageDrawable(imageDrawable);
									imageView.postInvalidate();
								}
							}
						}, false, bitmap.getWidth(), bitmap.getHeight(),
						context.getResources());
		if (bitmapDrawable == null) {
			thumbImageView.setImageResource(defaultPicId);
		} else {
			thumbImageView.setImageDrawable(bitmapDrawable);
			thumbImageView.postInvalidate();
		}
		bitmap.recycle();
	}

	/**
	 * description:���ͼƬ��ַ���ز�����ͼƬ<br/>
	 * modify note:<br/>
	 * author:Ryan.Van<br/>
	 * date:2015-2-5<br/>
	 * 
	 * @param context
	 *            �Ự������
	 * @param thumbImageView
	 *            ͼƬչʾ��ImageView
	 * @param strUrl
	 *            ͼƬ��ַ
	 * @param defaultPicId
	 *            Ĭ��չʾͼƬ
	 */
	@SuppressWarnings("deprecation")
	@SuppressLint("NewApi")
	public static void setImageBackgroundToImageView(Context context,
			ImageView thumbImageView, String strUrl, int defaultPicId) {
		final int version = android.os.Build.VERSION.SDK_INT;
		thumbImageView.setTag(strUrl);
		Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
				defaultPicId);
		BitmapDrawable bitmapDrawable = ImageAsyncLoader.getInstance()
				.loadBitmapDrawable(
						strUrl,
						thumbImageView,
						new ImageCallback() {

							@Override
							public void imageLoaded(
									BitmapDrawable imageDrawable,
									ImageView imageView, String imageUrl) {
								String curUrl = (String) imageView.getTag();
								if (imageUrl.equalsIgnoreCase(curUrl)) {
									if (version < 16) {
										imageView
												.setBackgroundDrawable(imageDrawable);
									} else {
										imageView.setBackground(imageDrawable);
									}
									imageView.postInvalidate();
								}
							}
						}, false, bitmap.getWidth(), bitmap.getHeight(),
						context.getResources());
		if (bitmapDrawable == null) {
			thumbImageView.setBackgroundResource(defaultPicId);
		} else {
			if (version < 16) {
				thumbImageView.setBackgroundDrawable(bitmapDrawable);
			} else {
				thumbImageView.setBackground(bitmapDrawable);
			}
			thumbImageView.postInvalidate();
		}
		bitmap.recycle();
	}

	public static void saveBitmap(String path, Bitmap bm, int quality) {

		FileOutputStream b = null;
		try {
			b = new FileOutputStream(path);
			bm.compress(Bitmap.CompressFormat.PNG, quality, b);// 把数据写入文件
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				// 关闭流
				if(b != null) {
					b.flush();
					b.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}

	}

}
